import axios from 'axios';

const API_URL = 'http://172.16.103.206:8081/api';

export interface Exam {
    id: string;
    title: string;
    description: string;
    duration: number;
    totalScore: number;
    passScore: number;
    questions: string[];
    createdBy: {
        id: string;
        username: string;
    };
    startTime: string;
    endTime: string;
    status: 'draft' | 'published' | 'ended';
    createdAt: string;
}

export interface CreateExamData {
    title: string;
    description: string;
    duration: number;
    totalScore: number;
    passScore: number;
    questions: string[];
    startTime: string;
    endTime: string;
}

export interface UpdateExamData extends Partial<CreateExamData> {
    status?: 'draft' | 'published' | 'ended';
}

// 创建考试
export const createExam = async (data: CreateExamData): Promise<Exam> => {
    const response = await axios.post(`${API_URL}/exams`, data, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
    return response.data;
};

// 获取考试列表
export const getExams = async (): Promise<Exam[]> => {
    const response = await axios.get(`${API_URL}/exams`, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
    return response.data;
};

// 获取单个考试详情
export const getExamById = async (id: string): Promise<Exam> => {
    const response = await axios.get(`${API_URL}/exams/${id}`, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
    return response.data;
};

// 更新考试
export const updateExam = async (id: string, data: UpdateExamData): Promise<Exam> => {
    const response = await axios.put(`${API_URL}/exams/${id}`, data, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
    return response.data;
};

// 删除考试
export const deleteExam = async (id: string): Promise<void> => {
    await axios.delete(`${API_URL}/exams/${id}`, {
        headers: {
            Authorization: `Bearer ${localStorage.getItem('token')}`
        }
    });
}; 